<?php

namespace app\controller\admin\sgz;

use app\model\admin\sgz\SgzWarMethods;
use app\model\admin\sgz\SgzWarMethodsGenerals;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class Career
 * @package app\controller\admin\ah
 */
class WarMethods extends Backend
{
    protected $model;
    public $orderRule = ['id' => 'DESC'];

    public function initialize()
    {
        $this->model = new SgzWarMethods();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function index()
    {
        $where = $this->buildSearchParams();
        $order = $this->buildOrder();
        $allData = $this->request->param('all_data');

        if ($allData) {
            $data = $this->model->where($where)->order($order)->cache(true)->select()->toArray();
        } else {
            $limit = $this->request->param('limit', 10);
            $data = $this->model->where($where)->order($order)->paginate($limit)->toArray();
            foreach ($data['data'] as &$item) {
                switch ($item['form']) {
                    case 1:
                        $item['form_name'] = "自带战法";
                        break;
                    case 2:
                        $item['form_name'] = "传承战法";
                        break;
                    case 3:
                        $item['form_name'] = "事件战法";
                        break;
                    default:
                        $item['form_name'] = "";
                }
                if (isset($item['url']) && !empty($item['url'])) {
                    $item['url'] = imgUrlToAbsolute($item['url']);
                }
                if (isset($item['icon']) && !empty($item['icon'])) {
                    $item['icon'] = imgUrlToAbsolute($item['icon']);
                }
            }
        }
        return $this->success('数据获取成功', $data);
    }

    //添加
    public function add()
    {
        $post = CommonFun::filterPostData($this->request->post());
        try {
            $data =[
              'name' =>$post['name'],
              'icon' =>$post['icon'],
              'picture' =>$post['picture'],
              'level' =>$post['level'],
              'category' =>$post['category'],
              'form' =>$post['form'],
              'arms' =>$post['arms'],
              'effect_conflict' =>$post['effect_conflict'],
              'methods_effect' =>$post['methods_effect'],
              'methods_target' =>$post['methods_target'],
              'launch_probability' =>$post['launch_probability'],
              'methods_description' =>$post['methods_description'],
              'create_time' =>date("Y-m-d H:i:s"),
            ];
            $id = $this->model->insertGetId($data);
            $methodsGenerals = [];
            if(isset($post['generals_one'])&!empty($post['generals_one'])){
                $post['generals_one'] = explode(",",$post['generals_one']);
                foreach ($post['generals_one'] as $item){
                    $methodsGenerals[] =[
                        'wm_id' =>$id,
                        'generals_id' =>$item,
                        'form' =>1,
                        'create_time' =>date("Y-m-d H:i:s"),
                    ];
                }
            }
            if(isset($post['generals_two'])&!empty($post['generals_two'])){
                $post['generals_two'] = explode(",",$post['generals_two']);
                foreach ($post['generals_two'] as $item){
                    $methodsGenerals[] =[
                        'wm_id' =>$id,
                        'generals_id' =>$item,
                        'form' =>2,
                        'create_time' =>date("Y-m-d H:i:s"),
                    ];
                }
            }
            if(count($methodsGenerals)>0)(new SgzWarMethodsGenerals())->insertAll($methodsGenerals);
            $this->clearCache();
            return $this->success('添加成功', $post);
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    //添加
    public function edit()
    {
        $post = CommonFun::filterPostData($this->request->post());
        try {
            $data =[
                'id' => $post['id'],
                'name' =>$post['name'],
                'icon' =>$post['icon'],
                'picture' =>$post['picture'],
                'level' =>$post['level'],
                'category' =>$post['category'],
                'form' =>$post['form'],
                'arms' =>$post['arms'],
                'effect_conflict' =>$post['effect_conflict'],
                'methods_effect' =>$post['methods_effect'],
                'methods_target' =>$post['methods_target'],
                'launch_probability' =>$post['launch_probability'],
                'methods_description' =>$post['methods_description'],
            ];
            $this->model->update($data);
            $id = $post['id'];
            $methodsGenerals = [];
            if(isset($post['generals_one'])&!empty($post['generals_one'])){
                $post['generals_one'] = explode(",",$post['generals_one']);
                foreach ($post['generals_one'] as $item){
                    $methodsGenerals[] =[
                        'wm_id' =>$id,
                        'generals_id' =>$item,
                        'form' =>1,
                        'create_time' =>date("Y-m-d H:i:s"),
                    ];
                }
            }
            if(isset($post['generals_two'])&!empty($post['generals_two'])){
                $post['generals_two'] = explode(",",$post['generals_two']);
                foreach ($post['generals_two'] as $item){
                    $methodsGenerals[] =[
                        'wm_id' =>$id,
                        'generals_id' =>$item,
                        'form' =>2,
                        'create_time' =>date("Y-m-d H:i:s"),
                    ];
                }
            }
            (new SgzWarMethodsGenerals())->where('wm_id',$id)->delete();
            if(count($methodsGenerals)>0)(new SgzWarMethodsGenerals())->insertAll($methodsGenerals);
            $this->clearCache();
            return $this->success('添加成功', $post);
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    //查看详情
    public function info()
    {
        $id   = $this->request->param('id');
        $info = $this->model->find($id);
        if(is_object($info))$info = $info->toArray();
        $list = (new SgzWarMethodsGenerals())->where('wm_id',$id)->field("generals_id,form")->select()->toArray();
        $info['generals_one'] =[];
        $info['generals_two'] =[];
        foreach ($list as $item){
            if($item['form']==1){
                array_push($info['generals_one'],$item['generals_id']);
            }else{
                array_push($info['generals_two'],$item['generals_id']);
            }
        }
        $info['generals_one'] = implode(",",$info['generals_one']);
        $info['generals_two'] = implode(",",$info['generals_two']);
        return $this->success('获取成功', $info);
    }

    //设置展示
    public function setIsShow()
    {
        $id = $this->request->post('id');
        $fieldVal = $this->request->post('field_val');
        $isRecycle = $this->request->post('is_recycle');
        $update['is_show'] = $fieldVal;

        try {
            if ($isRecycle) {
                $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
            } else {
                $updateRes = $this->model->where('id', '=', $id)->update($update);
            }

            if ($updateRes) {
                $this->clearCache();
                return $this->success('操作成功');
            } else if ($updateRes === 0) {
                return $this->success('未作修改');
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    public function getCategory()
    {
        return $this->success('获取战法类型', [
            ['category' => '主动'],
            ['category' => '被动'],
            ['category' => '指挥'],
            ['category' => '阵法'],
            ['category' => '增益'],
            ['category' => '突击'],
            ['category' => '内政'],
            ['category' => '兵种'],
        ]);
    }

    public function getArms()
    {
        return $this->success('获取兵种', [
            ['arm' => '骑兵'],
            ['arm' => '盾兵'],
            ['arm' => '弓兵'],
            ['arm' => '枪兵'],
            ['arm' => '器械'],
        ]);
    }
}